package com.mjh.zhujianju.service.party.impl;

import com.google.common.collect.Lists;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.mjh.zhujianju.dao.party.BuildingExamTopicMapper;
import com.mjh.zhujianju.model.party.BuildingExamAnswerEO;
import com.mjh.zhujianju.model.party.BuildingExamTopicEO;
import com.mjh.zhujianju.model.party.BuildingExamUserEO;
import com.mjh.zhujianju.service.party.BuildingExamAnswerService;
import com.mjh.zhujianju.service.party.BuildingExamResultService;
import com.mjh.zhujianju.service.party.BuildingExamTopicService;
import com.mjh.zhujianju.service.party.BuildingExamUserService;
import com.mjh.zhujianju.util.party.ExamUtil;
import com.mjh.zhujianju.vo.party.BuildingExamResultVO;
import com.mjh.zhujianju.vo.party.BuildingExamTopicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuyang
 * @Description: 试题业务
 * @date 2018/8/18 14:19
 */
@Service
public class BuildingExamTopicServiceImpl extends BaseServiceImpl<BuildingExamTopicEO> implements BuildingExamTopicService {

    @Autowired
    private BuildingExamTopicMapper buildingExamTopicMapper;

    @Autowired
    private BuildingExamAnswerService buildingExamAnswerService;

    @Resource
    private BuildingExamResultService buildingExamResultService;

    @Autowired
    private ExamUtil examUtil;

    public BuildingExamTopicServiceImpl(BuildingExamTopicMapper buildingExamTopicMapper) {
        super(buildingExamTopicMapper);
    }

    @Override
    public List<BuildingExamTopicVO> queryBuildingExamTopicForWXList(Long id) {
        //查询当前科目下面所有试题
        List<BuildingExamTopicEO> buildingExamTopicEOList = buildingExamTopicMapper.selectTopicList(id);
        if (CollectionUtils.isEmpty(buildingExamTopicEOList)){
            return null;
        }
        //得到所有试题id
        List<Long> topicIds = buildingExamTopicEOList.stream().map(buildingExamTopicEO1 -> buildingExamTopicEO1.getId())
                .distinct().collect(Collectors.toList());

        //根据试题id查询试题内容
        List<BuildingExamAnswerEO> buildingExamAnswerEOList= buildingExamAnswerService.queryBuildingExamAnswerByTopicIds(topicIds);
        if (CollectionUtils.isEmpty(buildingExamAnswerEOList)){
            return null;
        }

        //组装试题
        List<BuildingExamTopicVO> list = examUtil.initTopicAnswers(buildingExamTopicEOList,buildingExamAnswerEOList);
        return list;
    }

    @Override
    public List<BuildingExamTopicVO> selectTopicAnswers(Long examId) {
        return buildingExamTopicMapper.selectTopicAnswers(examId);
    }

    @Override
    public List<BuildingExamTopicEO> queryTopicByIds(List<Long> ids) {
        return buildingExamTopicMapper.queryTopicByIds(ids);
    }

    @Override
    public List<BuildingExamTopicVO> queryExamResult(Long examId, Long userId) {

        //查询当前课程所有题目
        List<BuildingExamTopicVO>  examTopicVOList = queryBuildingExamTopicForWXList(examId);

        //清除正确答案
        examTopicVOList.stream().forEach(buildingExamTopicVO ->
                buildingExamTopicVO.getBuildingExamAnswerEOList().stream()
                        .forEach(buildingExamAnswerEO -> buildingExamAnswerEO.setIsAnswer("1")));

        //查询对的题目
        List<BuildingExamTopicVO> trueTopicList = buildingExamTopicMapper.selectTopicAnswers(examId);

        //正确答案
        for(BuildingExamTopicVO vo1 : examTopicVOList){
            String answer = "";
            for (BuildingExamTopicVO vo2 : trueTopicList){
                if (vo1.getId().equals(vo2.getId())){
                    if (StringUtils.isEmpty(answer)){
                        answer = vo2.getSort();
                    }else {
                        answer = answer +"、" +vo2.getSort();
                    }
                }
            }
            vo1.setSort(answer);
        }

        //查询用户题目记录
        BuildingExamResultVO buildingExamResultVO = buildingExamResultService.queryBuildingExamResultList(examId,userId);

        for(BuildingExamTopicVO vo : examTopicVOList){
            List<BuildingExamAnswerEO> list = vo.getBuildingExamAnswerEOList();
            for (BuildingExamAnswerEO buildingExamAnswerEO : list){
                for (BuildingExamUserEO eo : buildingExamResultVO.getList()){
                    if (buildingExamAnswerEO.getId().equals(eo.getAnswerId())){
                        buildingExamAnswerEO.setIsAnswer("0");
                        continue;
                    }
                }
            }
        }
        return examTopicVOList;
    }

    @Override
    public List<BuildingExamTopicEO> selectPagination(BuildingExamTopicEO buildingExamTopicEO) {
        return buildingExamTopicMapper.selectPagination(buildingExamTopicEO);
    }

    @Override
    public BuildingExamTopicEO selectTpoicInfo(Long id) {
        BuildingExamTopicEO buildingExamTopicEO = buildingExamTopicMapper.selectByPrimaryKey(id);
        return buildingExamTopicEO;
    }

    @Override
    public List<BuildingExamTopicEO> findBuildingExamTopicByExamId(Long examId) {
        return buildingExamTopicMapper.selectTopicList(examId);
    }
}
